package lcs.table;

public class TableGenerator {

	/**
	 * @param string1
	 * @param string2
	 * @return The generated FinderTable.
	 */
	public static FinderTable generateTable(String string1, String string2)
	{
		FinderTable table = new FinderTable(string1, string2);
		for (int y=0;y<string2.length();y++) { //for each row
			int currCount=0; 
			if (y>0) //if this is not the first row, get initial count from previous row
				currCount=table.getElementAt(0,y-1).getSubstructureLength();
			for (int x=0;x<string1.length();x++) { //for each column
				//TODO: clean up this algorithm
				if ( //if the previous row incremented at this spot
						x>0 && y>0 && x<=string1.length() && y<=string2.length() && //don't check if this is the first row/column
						table.getElementAt(x,y-1).getSubstructureLength() != //don't increment if we're already equal
						currCount &&
						table.getElementAt(x,y-1).getSubstructureLength() > 
						table.getElementAt(x-1,y-1).getSubstructureLength())
					currCount++;
				else if (string2.charAt(y)==string1.charAt(x)) //if the letters match
					currCount++; //increment the current row's counter
				table.setElementAt(x, y, new TableElement(currCount));
			}
		}
		return table;
	}
}
